package com.adgox.tiantianbiting.home.index;

import com.adgox.tiantianbiting.bean.BaseResponseBean;
import com.adgox.tiantianbiting.bean.MidTabBean;
import com.adgox.tiantianbiting.bean.PageBean;
import com.adgox.tiantianbiting.bean.ProgramBean;
import com.adgox.tiantianbiting.bean.SubProgramBean;
import com.adgox.tiantianbiting.bean.TopBannerBean;
import com.adgox.tiantianbiting.bean.UserInfo;
import com.adgox.tiantianbiting.net.callback.JsonCallback;
import com.adgox.tiantianbiting.utils.ApiConstant;
import com.adgox.tiantianbiting.utils.DialogUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.GetRequest;
import com.rae.swift.session.SessionManager;

import java.util.List;

/**
 * Created by tang on 2018/12/24.
 */

public class HomeIndexPresenterImpl implements HomeIndexContract.Presenter {

    private HomeIndexContract.View mView;
    private int userId = SessionManager.getDefault().getUser() == null?0:((UserInfo)SessionManager.getDefault().getUser()).getId();

    public HomeIndexPresenterImpl(HomeIndexContract.View mView) {
        this.mView = mView;
        mView.setPresenter(this);
    }

    @Override
    public void start() {
        //首页初始化动作
        getBanner();
        getMidTab();
        getMidBanner();
        getHot();
        getProgram();
    }


    //获取首页banner信息
    private void getBanner() {
        OkGo.<BaseResponseBean<List<TopBannerBean>>>get(ApiConstant.BASE_URL+ApiConstant.HOME)
                .params("type",1)
                .execute(new JsonCallback<BaseResponseBean<List<TopBannerBean>>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<List<TopBannerBean>>> response) {
                        if (!"0".equals(response.body().getCode())) {
                            mView.onGetTopBannerFailed(response.body().getMessage());
                            return;
                        }
                        List<TopBannerBean> topBannerList = response.body().getData();
                        mView.onSetTopBanner(topBannerList);
                    }
                });
    }

    //获取中间导航栏信息
    private void getMidTab() {
        OkGo.<BaseResponseBean<List<MidTabBean>>>get(ApiConstant.BASE_URL+ApiConstant.HOME)
                .params("type",2)
                .execute(new JsonCallback<BaseResponseBean<List<MidTabBean>>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<List<MidTabBean>>> response) {
                        if (!"0".equals(response.body().getCode())) {
                            mView.onGetMidTabFailed(response.body().getMessage());
                            return;
                        }
                        List<MidTabBean> midTabList = response.body().getData();
                        mView.onSetMidTab(midTabList);
                    }
                });
    }

    //获取中部banner信息
    private void getMidBanner(){
        //第一版不做
    }

    //获取热门内容(只获取前三项)
    private void getHot() {
        GetRequest<BaseResponseBean<PageBean<SubProgramBean>>> baseRequest = null;
        if (userId <= 0){
            baseRequest = OkGo.<BaseResponseBean<PageBean<SubProgramBean>>>get(ApiConstant.BASE_URL + ApiConstant.GET_PROGRAM_BY_CATEGOTY + 41);
        } else {
            baseRequest = OkGo.<BaseResponseBean<PageBean<SubProgramBean>>>get(ApiConstant.BASE_URL + ApiConstant.GET_PROGRAM_BY_CATEGOTY + 41)
                    .params("userId", this.userId);
        }
        baseRequest.params("startPoint",1)
                .params("maxPage",3)
                .execute(new JsonCallback<BaseResponseBean<PageBean<SubProgramBean>>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<PageBean<SubProgramBean>>> response) {
                        if (!"0".equals(response.body().getCode())) {
                            mView.onGetHotFailed(response.body().getMessage());
                            return;
                        }
                        PageBean<SubProgramBean> hotList = response.body().getData();
                        mView.onSetHot(hotList);
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<PageBean<SubProgramBean>>> response) {
                        super.onError(response);
                    }
                });
    }


    //获取栏目内容
    private void getProgram() {
        OkGo.<BaseResponseBean<List<ProgramBean>>>get(ApiConstant.BASE_URL+ApiConstant.GET_PROGRAM)
                .params("userId",userId)
                .execute(new JsonCallback<BaseResponseBean<List<ProgramBean>>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<List<ProgramBean>>> response) {
                        DialogUtils.dismiss();
                        if (!"0".equals(response.body().getCode())) {
                            mView.onGetProgramFailed(response.body().getMessage());
                            return;
                        }
                        List<ProgramBean> programList = response.body().getData();
                        mView.onSetProgram(programList);
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<List<ProgramBean>>> response) {
                        super.onError(response);
                        DialogUtils.dismiss();
                    }
                });
    }

}
